package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Order;
import com.atguigu.service.OrderService;
import com.atguigu.service.OrdersettingService;
import com.atguigu.utils.SMSUtils;
import com.atguigu.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @author Eric
 * @Create 2020-09-20  15:06
 * @Version project-workspace-0912
 **/
@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

   @RequestMapping("send4Order")
    public Result send4Order(String telephone)
   {
       Integer code = ValidateCodeUtils.generateValidateCode(4);
       try {
           //发送验证码
           SMSUtils.sendShortMessage(telephone,code.toString());

       } catch (Exception e) {
           e.printStackTrace();
           return new Result(true, MessageConstant.SEND_VALIDATECODE_FAIL);
       }
       System.out.println("发送的手机验证码 = " + code);
       //将生成的验证码缓存到redis中
       jedisPool.getResource().setex(
               telephone+ RedisMessageConstant.SENDTYPE_ORDER,5*60,code.toString());
       //验证码发送成功
       return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
   }

   @RequestMapping("/send4Login")
    public Result send4Login(String telephone)
   {
       Integer code = ValidateCodeUtils.generateValidateCode(4);//生成4位验证码
       try {
           SMSUtils.sendShortMessage(telephone,code.toString());
       } catch (Exception e) {
           e.printStackTrace();
           //验证码发送失败
           return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
       }

       System.out.println("code = " + code);
        //将生成的验证码缓存到redis中
       jedisPool.getResource().setex(telephone+RedisMessageConstant.SENDTYPE_LOGIN,5*60,code.toString());
      //验证码发送成功！
       return new Result(true,MessageConstant.SEND_VALIDATECODE_SUCCESS);
   }


}
